import requests
import re

url = "https://www.shanghairanking.cn/_nuxt/static/1636358336/rankings/bcur/2021/payload.js"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62"}

response = requests.get(url, headers=headers)
html = response.content.decode()
school_name_pattern = 'univNameCn:"(.*?)"'
school_name_com = re.compile(school_name_pattern, re.S)
school_name_list = re.finditer(school_name_com, html)
key_pattern = 'function(.*?){'
key_com = re.compile(key_pattern, re.S)
key_list = re.search(key_com, html).group(1)[1:-1].split(',')
value_pattern = 'mutations:.*?}}\((.*?)\)\)\);'
value_com = re.compile(value_pattern, re.S)
value_list = re.search(value_com, html).group(1).split(',')
value_list = [x.strip('"') for x in value_list]
value_list[value_list.index('2021')] = value_list[value_list.index('2021')] + ',' + value_list[value_list.index('2020')]
value_list.remove('2020')
province_pattern = 'province:(.*?),'
province_com = re.compile(province_pattern, re.S)
province_list = re.finditer(province_com, html)
type_pattern = 'univCategory:(.*?),'
type_com = re.compile(type_pattern, re.S)
type_list = re.finditer(type_com, html)
score_pattern = 'score:(.*?),'
score_com = re.compile(score_pattern,re.S)
score_list = re.finditer(score_com, html)
# for i in school_name_list:
#     print(i.group(1))
params_dict = {}
for i, j in zip(key_list, value_list):
    params_dict[i] = j
# for i in province_list:
#     school_province = params_dict[i.group(1)]
#     print(school_province)
# for i in type_list:
#     school_type = params_dict[i.group(1)]
#     print(school_type)
# for i in score_list:
#     score = i.group(1)
#     if score.isalpha():
#         print(params_dict[score])
#     else:
#         print(score)
for i in score_list:
    score = i.group(1)
    if score.isdigit():
        print(score)
    else:
        print(params_dict[score])
